/**
 * Created by IntelliJ IDEA.
 * User: alex
 * Date: 9/6/12
 * Time: 11:39 AM
 * To change this template use File | Settings | File Templates.
 */
package org.kolonitsky.alexey.ui
{

    import flash.display.Bitmap;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.filters.DropShadowFilter;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFieldType;
    import flash.text.TextFormat;

    public class SimpleButton extends Sprite
    {
        /** Dafault value of height **/
        protected const DEFAULT_HEIGHT: Number = 22;

        /** Default value of width **/
        protected const DEFAULT_WIDTH: Number = 100;

        /** Width **/
        protected var _width: Number;

        /** Height **/
        protected var _height: Number;



        protected var bitmap:Bitmap;

        //-----------------------------
        // label
        //-----------------------------

        private var _label:TextField = new TextField();

        /** Property setter on _text field **/
        public function set label (value:String): void
        {
            _label.text = value;
        }

        /** Property getter on _text field **/
        public function get label (): String
        {
            return _label.text;
        }

        /** Constructor
         * @param caption text on buttonMode
         * @param autoSize ability change widht and heithgt parameter of buttonMode
         * @param w width
         * @param h height
         */
        public function SimpleButton(caption:String = "", bitmap:Bitmap = null,
            autoSize:Boolean=true, w:Number=DEFAULT_WIDTH, h:Number=DEFAULT_HEIGHT)
        {
            super();

            if (caption)
            {
                _label = new TextField();
                _label.selectable = false;
                _label.type = TextFieldType.DYNAMIC;
                _label.autoSize = TextFieldAutoSize.LEFT;
                _label.multiline = false;
                _label.defaultTextFormat = new TextFormat("_sans", "12", 0xFFFFFF, true);
                _label.text = caption;
                _label.mouseEnabled = false;
                _label.x = 10;
                addChild(_label);
            }

            if (autoSize)
            {
                _width = _label.width + 20;
                _height = _label.height;
            }
            else
            {
                _width = w;
                _height = h;
                _label.width = w;
                _label.height = h;
            }

            if (bitmap)
            {
                this.bitmap = bitmap;
                addChild(bitmap);
            }

            this.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
            this.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
            this.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
            this.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);

            this.buttonMode = true;
            this.useHandCursor = true;
            this.visible = true;
            this.mouseEnabled = true;
            this.mouseChildren = false;
        }

        private function mouseDownHandler(event:MouseEvent):void
        {
            bitmap.alpha = 1.0;
            filters = [new DropShadowFilter(2, 90, 0x000000, 0.7, 4, 4)]
        }

        private function mouseUpHandler(event:MouseEvent):void
        {
            bitmap.alpha = 1.0;
            filters = [new DropShadowFilter(2, 90, 0x000000, 0.7, 4, 4)]
        }

        private function mouseOverHandler(event:MouseEvent):void
        {
            _label.textColor = 0xFFFFFF
            filters = [new DropShadowFilter(2, 90, 0x000000, 0.7, 4, 4)]
        }

        private function mouseOutHandler(event:MouseEvent):void
        {
            _label.textColor = 0xFFFFFF
            filters = [];
        }
    }
}
